package com.freelance.vn.apps.puzzle.view;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;

public class CoverFlow  extends Gallery{
	/**
	 * Graphic camera used for transforming the matrix of ImageView
	 */
	private Camera mCamera =new Camera();
	/**
	 * the maximum angle the child ImageView will be rotated by
	 */
	private int mMaxRotationAngle=60;
	
	/**
	 * the maximum zoom on the center child
	 */
	private int mMaxZoom=-120;
	/**
	 * the center of Coverflow
	 */
	private int mCoverflowCenter;
	public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public CoverFlow(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public CoverFlow(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	/**
	 * 
	 * @return maximum angle the child ImageView rotated by
	 */
	public int getmMaxRotationAngle() {
		return mMaxRotationAngle;
	}
	/**
	 * set maximum angle the child ImageView rotated by
	 * @param mMaxRotationAngle
	 */
	public void setmMaxRotationAngle(int mMaxRotationAngle) {
		this.mMaxRotationAngle = mMaxRotationAngle;
	}
	/**
	 * get maximum zoom on center child
	 * @return
	 */
	public int getmMaxZoom() {
		return mMaxZoom;
	}
	/**
	 * set maximum zoom on center child
	 * @param mMaxZoom
	 */
	public void setmMaxZoom(int mMaxZoom) {
		this.mMaxZoom = mMaxZoom;
	}
	/**
	 * Get center of Coverflow
	 * @return get centerOfCoverflow
	 */
	private int getCenterOfCoverflow(){
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();

	}
	/**
	 * Get center of given View
	 * @param v
	 * @return center of View v
	 */
	private static int getCenterOfView(View v){
		
		return(( v.getLeft()+v.getRight())/2);
	}
	
	@Override
	/**
	 * 
	 */
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		mCoverflowCenter=getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}
	
	
	private void  transformImageBitmap(ImageView child,Transformation inf,int rotationAngle){
		mCamera.save();
		final Matrix imageMatrix = inf.getMatrix();
		final int imgHeight=child.getLayoutParams().height;
		final int imgWidth=child.getLayoutParams().width;
		final int rotation= Math.abs(rotationAngle);
		mCamera.translate(0.0f, 0.0f, 100.0f);
		
		if(rotation<mMaxRotationAngle){
			float zoomAmount=(float)(mMaxZoom+ rotation*1.5);
			mCamera.translate(0.0f,0.0f, zoomAmount);
		}
		
		mCamera.rotateY(rotationAngle);
		mCamera.getMatrix(imageMatrix);
		imageMatrix.preTranslate(-(imgWidth/2), -(imgHeight/2));
		imageMatrix.postTranslate(imgWidth/2, imgHeight/2);
		
		mCamera.restore();
	}
	
	protected boolean getChildStaticTransformation(View child, Transformation inf){
		final int childCenter=getCenterOfView(child);
		final int childWidth=child.getWidth();
		int rotationAngle=0;
		
		inf.clear();
		inf.setTransformationType(Transformation.TYPE_MATRIX);
			
		if(childCenter==mCoverflowCenter){
			transformImageBitmap((ImageView) child, inf, rotationAngle);
		}else{
			rotationAngle = (int) (((float) (mCoverflowCenter - childCenter)/ childWidth) *  mMaxRotationAngle);
			if(Math.abs(rotationAngle)>mMaxRotationAngle){
				rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;  
			}
			
			transformImageBitmap((ImageView)child, inf, rotationAngle);
		}
		
		
		return true;
	}
	
}
